package com.cmc.cloud.cmclink.doc.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.vo.BaseCodeCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.BaseCodePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.BaseCodeRespVO;
import com.cmc.cloud.cmclink.doc.vo.BaseCodeUpdateReqVO;
import com.cmes.framework.common.pojo.CommonResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import java.util.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;


import com.cmes.framework.common.pojo.PageResult;

import com.cmc.cloud.cmclink.doc.entity.BaseCodeDO;
import com.cmc.cloud.cmclink.doc.service.BaseCodeService;


/**
 * {table.classComment}前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 基础数据配置信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL+"/baseCode")
public class BaseCodeController {

    @Resource
    private BaseCodeService baseCodeService;

    @Operation(summary = "创建基础数据配置信息")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission({'doc:packagecode:add','doc:cargotype:add','doc:signtype:add','doc:vgmtype:add','doc:dotype:add','doc:sostatus:add','doc:sistatus:add','doc:customsChangeReason:add'})")
    public CommonResult<Void> createBaseCode(@Valid @RequestBody BaseCodeCreateReqVO createReqVO) {
        baseCodeService.createBaseCode(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新基础数据配置信息")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission({'doc:packagecode:edit','doc:packagecode:enable','doc:cargotype:edit','doc:cargotype:enable','doc:signtype:edit','doc:signtype:enable','doc:vgmtype:edit','doc:vgmtype:enable','doc:dotype:edit','doc:dotype:enable','doc:sostatus:edit','doc:sostatus:enable','doc:sistatus:edit','doc:sistatus:enable','doc:customsChangeReason:edit'})")
    public CommonResult<Boolean> updateBaseCode(@Valid @RequestBody BaseCodeUpdateReqVO updateReqVO) {
        baseCodeService.updateBaseCode(updateReqVO);
        return CommonResult.success(true);
    }

    @Operation(summary = "删除基础数据配置信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:packagecode:delete','doc:cargotype:delete','doc:signtype:delete','doc:vgmtype:delete','doc:dotype:delete','doc:sostatus:delete','doc:sistatus:delete','doc:customsChangeReason:delete'})")
    public CommonResult<Void> deleteBaseCode(@PathVariable("id") Long id) {
        baseCodeService.deleteBaseCode(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得基础数据配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @GetMapping(value = "/getById/{id}")
    public CommonResult<BaseCodeRespVO> getBaseCode(@PathVariable("id") Long id) {
        BaseCodeDO baseCode = baseCodeService.getBaseCode(id);
        return CommonResult.success(BeanUtil.copyProperties(baseCode, BaseCodeRespVO.class));
    }

    @Operation(summary = "获得基础数据配置信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PostMapping("/listAll")
    public CommonResult<List<BaseCodeRespVO>> getBaseCodeList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseCodeDO> list = baseCodeService.getBaseCodeList(ids);
        return CommonResult.success(BeanUtil.copyToList(list, BaseCodeRespVO.class));
    }

    @Operation(summary = "通过代码类型获取基础数据配置")
    @Parameter(name = "codeCategory", description = "编码类型", required = true)
    @GetMapping("/listAllByType")
    public CommonResult<List<BaseCodeRespVO>> getBaseCodeListByType(@RequestParam("codeCategory") String codeCategory) {
        List<BaseCodeDO> list = baseCodeService.getBaseCodeListByType(codeCategory);
        return CommonResult.success(BeanUtil.copyToList(list, BaseCodeRespVO.class));
    }

    @Operation(summary = "获得基础数据配置信息分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission({'doc:packagecode:search','doc:cargotype:search','doc:signtype:search','doc:vgmtype:search','doc:dotype:search','doc:sostatus:search','doc:sistatus:search','doc:customsChangeReason:search'})")
    public CommonResult<PageResult<BaseCodeRespVO>> getBaseCodePage(@Valid @RequestBody BaseCodePageReqVO pageVO) {
        PageResult<BaseCodeDO> pageResult = baseCodeService.getBaseCodePage(pageVO);
        List<BaseCodeRespVO> results = BeanUtil.copyToList(pageResult.getList(), BaseCodeRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "获得基础数据有效信息分页")
    @PostMapping("enabledInfo/getPage")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<BaseCodeRespVO>> getEnabledBaseCodePage(@Valid @RequestBody BaseCodePageReqVO pageVO) {
        PageResult<BaseCodeDO> pageResult = baseCodeService.getEnabledBaseCodePage(pageVO);
        List<BaseCodeRespVO> results = BeanUtil.copyToList(pageResult.getList(), BaseCodeRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }


    @Operation(summary = "获得基础数据配置信息分页-无权限限制")
    @PostMapping("/getPage/NoAuth")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<BaseCodeRespVO>> getBaseCodePageNoAuth(@Valid @RequestBody BaseCodePageReqVO pageVO) {
        PageResult<BaseCodeDO> pageResult = baseCodeService.getBaseCodePage(pageVO);
        List<BaseCodeRespVO> results = BeanUtil.copyToList(pageResult.getList(), BaseCodeRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
